import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
/**
 * A System Administrator Panel class.
 * @author Team 1: Thunder/Dragon
 *
 */

public class SystemAdminPanel extends JPanel {
	private static Logger systemAdminPanelLog = 
		Logger.getLogger(SystemAdminPanel.class.getName());

	private JButton btnEditUser;
	
	private JButton btnResAcc;
	
	private JButton btnCreateUser;
	
	private JButton btnFindUser;
	
	private JButton btnDeleteUser;
	
	private JButton btnLockAcc;
	
	private JButton btnUnlockAcc;

	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.ALL);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		systemAdminPanelLog.addHandler(console);
		systemAdminPanelLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("SystemAdminPanelLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
			systemAdminPanelLog.addHandler(fh);
		} catch (SecurityException e) {
			systemAdminPanelLog.severe("Failure: Security exception thrown " +
					"when trying to create a new handler " +
					"in SystemAdminPanel.java.");
			e.printStackTrace();
		} catch (IOException e) {
			systemAdminPanelLog.severe("Failure: I/O exception thrown" +
					" when trying to create a new handler" +
					" in SystemAdminPanel.java.");
			e.printStackTrace();
		}
		
	}
	
	private class ButtonListen implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(btnCreateUser)){
				btnCreateUser.getParent().firePropertyChange("createUser", 0, 1);
			}
			else if(e.getSource().equals(btnEditUser)){
				btnEditUser.getParent().firePropertyChange("editUser", 0, 1);
			}
			else if(e.getSource().equals(btnDeleteUser)){
				btnDeleteUser.getParent().firePropertyChange("deleteUser", 0, 1);
			}
			else if(e.getSource().equals(btnResAcc)){
				btnResAcc.getParent().firePropertyChange("resAcc", 0, 1);
			}
			else if(e.getSource().equals(btnLockAcc)){
				btnLockAcc.getParent().firePropertyChange("lockAcc", 0, 1);
			}
			else if(e.getSource().equals(btnUnlockAcc)){
				btnUnlockAcc.getParent().firePropertyChange("unlockAcc", 0, 1);
			}
			else if(e.getSource().equals(btnFindUser)){
				btnFindUser.getParent().firePropertyChange("findUser", 0, 1);
			}						
			
		}
		
		public String toString(){
			return "A string for the button.";
		}
		
	}
	
	/**
	 * This will represent the panel for the SystemAdmin.
	 */
	
	public SystemAdminPanel() {
		systemAdminPanelLog.entering(this.getClass().getName(),
				"SystemAdminPanel Constructor");
		setBackground(new Color(240, 240, 240));
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		btnCreateUser = new JButton("Create User");
		springLayout.putConstraint(SpringLayout.WEST,
				btnCreateUser, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				btnCreateUser, 140, SpringLayout.WEST, this);
		btnCreateUser.addActionListener(new ButtonListen());
		add(btnCreateUser);
		
		btnDeleteUser = new JButton("Delete User");
		springLayout.putConstraint(SpringLayout.WEST,
				btnDeleteUser, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST,
				btnDeleteUser, 140, SpringLayout.WEST, this);
		btnDeleteUser.addActionListener(new ButtonListen());
		add(btnDeleteUser);
		
		btnLockAcc = new JButton("Lock Account");
		springLayout.putConstraint(SpringLayout.WEST, 
				btnLockAcc, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST,
				btnLockAcc, 140, SpringLayout.WEST, this);
		btnLockAcc.addActionListener(new ButtonListen());
		add(btnLockAcc);
		
		btnResAcc = new JButton("Reset Password");
		springLayout.putConstraint(SpringLayout.NORTH,
				btnLockAcc, 6, SpringLayout.SOUTH, btnResAcc);
		springLayout.putConstraint(SpringLayout.EAST,
				btnResAcc, 140, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH,
				btnResAcc, 6, SpringLayout.SOUTH, btnDeleteUser);
		springLayout.putConstraint(SpringLayout.WEST,
				btnResAcc, 10, SpringLayout.WEST, this);
		btnResAcc.addActionListener(new ButtonListen());
		add(btnResAcc);
		
		btnEditUser = new JButton("Edit User");
		springLayout.putConstraint(SpringLayout.NORTH,
				btnDeleteUser, 4, SpringLayout.SOUTH, btnEditUser);
		springLayout.putConstraint(SpringLayout.EAST,
				btnEditUser, 140, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, 
				btnEditUser, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH,
				btnEditUser, 94, SpringLayout.NORTH, this);
		btnEditUser.addActionListener(new ButtonListen());
		add(btnEditUser);
		
		btnFindUser = new JButton("Find User");
		springLayout.putConstraint(SpringLayout.WEST, 
				btnFindUser, 0, SpringLayout.WEST, btnCreateUser);
		springLayout.putConstraint(SpringLayout.EAST,
				btnFindUser, 140, SpringLayout.WEST, this);
		btnFindUser.addActionListener(new ButtonListen());
		add(btnFindUser);
		
		JLabel lblAdministrative = new JLabel("Administrative");
		lblAdministrative.setFont(new Font("Pristina", Font.ITALIC, 18));
		lblAdministrative.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.NORTH,
				lblAdministrative, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST,
				lblAdministrative, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST,
				lblAdministrative, 139, SpringLayout.WEST, this);
		add(lblAdministrative);
		
		JLabel lblMenu = new JLabel("Menu");
		springLayout.putConstraint(SpringLayout.WEST,
				lblMenu, 10, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST,
				lblMenu, -11, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.NORTH,
				btnCreateUser, 4, SpringLayout.SOUTH, lblMenu);
		lblMenu.setFont(new Font("Pristina", Font.ITALIC, 18));
		springLayout.putConstraint(SpringLayout.NORTH,
				lblMenu, 7, SpringLayout.SOUTH, lblAdministrative);
		lblMenu.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblMenu);
		
		btnUnlockAcc = new JButton("Unlock Account");
		springLayout.putConstraint(SpringLayout.NORTH, btnFindUser, 6,
				SpringLayout.SOUTH, btnUnlockAcc);
		springLayout.putConstraint(SpringLayout.EAST, btnUnlockAcc, 0,
				SpringLayout.EAST, btnCreateUser);
		springLayout.putConstraint(SpringLayout.NORTH, btnUnlockAcc, 4,
				SpringLayout.SOUTH, btnLockAcc);
		springLayout.putConstraint(SpringLayout.WEST, btnUnlockAcc, 0,
				SpringLayout.WEST, btnCreateUser);
		btnUnlockAcc.addActionListener(new ButtonListen());
		add(btnUnlockAcc);
		
		systemAdminPanelLog.exiting(this.getClass().getName(), 
				"SystemAdminPanel Constructor");
	}

	public JButton getBtnNewButton_2() {
		return btnEditUser;
	}
	
	public JButton getBtnNewButton_4() {
		return btnResAcc;
	}
	
	public JButton getBtnNewButton() {
		return btnCreateUser;
	}
	
	public JButton getBtnSearchUser() {
		return btnFindUser;
	}
	
	public JButton getBtnNewButton_1() {
		return btnDeleteUser;
	}
	
	public JButton getBtnNewButton_3() {
		return btnLockAcc;
	}
	
	public JButton getBtnUnlockAcc(){
		return btnUnlockAcc;
	}
}
